namespace SubhadraSolutions.Sharp.Logging
{
    using SubhadraSolutions.Sharp.Utils.Diagnostics;
    using System;
    using System.Diagnostics;
    using System.Globalization;

    #region Delegates

    public delegate void WriteDelegate(string writer);

    #endregion Delegates

    internal class ConsoleDebugTraceLogCore
    {
        #region Fields

        private string _linePadding = "\r\n";
        private long _logItemIndex;

        #endregion Fields

        #region Constructors

        public ConsoleDebugTraceLogCore()
        {
            Target = LogTarget.Debug | LogTarget.Trace;
            if (ProcessHelper.CurrentProcessType == ProcessType.ConsoleApplication)
            {
                Target |= LogTarget.Console;
            }
            ExcludeNullContextValues = false;
        }

        #endregion Constructors

        #region Properties

        public bool ExcludeNullContextValues
        {
            get;
            set;
        }

        public string LinePadding
        {
            get { return _linePadding; }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    _linePadding = "\r\n";
                }
                else
                {
                    value = value.Trim().Trim('\r', '\n');
                    _linePadding = "\r\n" + value + "\r\n";
                }
            }
        }

        public LogTarget Target
        {
            get;
            set;
        }

        public WriteDelegate WriterDelegate
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public void log(LogStoreItem item)
        {
            WriteDelegate writer = WriterDelegate;

            _logItemIndex++;

            if (writer != null)
            {
                writer(_logItemIndex.ToString(CultureInfo.InvariantCulture));
                writer("\t");
                writer(item.ToString());
                writer(_linePadding);
            }

            if ((Target & LogTarget.Console) != 0)
            {
                Console.Write(_logItemIndex.ToString(CultureInfo.InvariantCulture));
                Console.Write("\t");
                Console.Write(item.ToString());
                Console.Write(_linePadding);
            }

            if ((Target & LogTarget.Debug) != 0)
            {
                Debug.Write(_logItemIndex.ToString(CultureInfo.InvariantCulture));
                Debug.Write("\t");
                Debug.Write(item.ToString());
                Debug.Write(_linePadding);
            }

            if ((Target & LogTarget.Trace) != 0)
            {
                Trace.Write(_logItemIndex.ToString(CultureInfo.InvariantCulture));
                Trace.Write("\t");
                Trace.Write(item.ToString());
                Trace.Write(_linePadding);
            }
        }

        #endregion Methods
    }
}